#include "julia2.h"

namespace fractals {
	const float PI=3.14159265f;
	const int N=2000;

	static void circle(vec2 center, float radius) {
		const int N=360;

		glBegin(GL_LINE_LOOP);
		for(int i=0; i<N; i++) {
			float theta=2*PI*(float)i/(float)N;
			
			float x=center[0] + radius*cos(theta);
			float y=center[1] + radius*sin(theta);

			glVertex2f(x, y);
		}
		glEnd();
	}

	Julia2::Julia2() : ShaderFractal("julia.sl")
	{
		p[0]=p[1]=0;

		glEnable(GL_LINE_SMOOTH);
		glLineWidth(.5); check();

		glColor3d(1,0,0);
	}

	void Julia2::draw(bool& didClear) {
		use(shader()) {
			shader().setUniform("epsilon", pitch());
		}

		ShaderFractal::draw(didClear);

		/*complex<float> two(2), one(1);

		complex<float> z(p[0], p[1]), c(-.48176f, -.53165f), zprime(1);

		for(int i=0; i<N; i++) {
			zprime=two*z*zprime;
			z=z*z + c;

			if(abs(zprime) > 1000)
			{
				circle(p, 1);
				return;
			}
		}

		float absz=abs(z);

		float max=2*absz/abs(zprime)*log(absz);

		cout << max << endl;
		circle(p, max);*/
	}

	static vec2 unproject(int x, int y) {
		vec3 h;
		h[0]=(float)x; h[1]=(float)y; h[2]=0;

		h=::unproject(h);

		vec2 v;
		v[0]=h[0]; v[1]=h[1];
		return v;
	}

	void Julia2::mouseHover(int x, int y) {
		p=unproject(x, y);
	}
}
	